package com.woniu.config;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Snowflake;
import com.woniu.interceptor.LoginInterceptor;
import com.woniu.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.HashMap;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Bean
    public HashMap<String,String> hashMap(){
        return new HashMap<>(16);
    }
    @Bean
    public LineCaptcha lineCaptcha(){
        //定义图形验证码的长和宽
        return CaptchaUtil.createLineCaptcha(110, 40);
    }
    @Bean
    public Snowflake snowflake(){
        return new Snowflake();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .allowedHeaders("*")
                .exposedHeaders("*")
                .maxAge(3600);
    }

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] path = {
                "/",
                "/account/loginByCode",
                "/code/getCode",
                "/error",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/swagger-resources/**",
                "/favicon.ico",
                "/flowerInfo/upload",
                "/account/**"
        };
        registry.addInterceptor(this.loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(path);

        registry.addInterceptor(this.permissionInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(path);
    }
}
